package cn.com.kh;

import cn.com.elite.EliteBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class KHDYNUtil {
    private static final Log log = LogFactory.getLog(KHDYNUtil.class);
    private static String log_Prefix = "KHDYNUtil-";

    // 对List内容除重
    public static List<String> doListDist(List<String> list) {
        log.debug(log_Prefix + "去重前集合:" + list.toString());
        list = list.stream().distinct().collect(Collectors.toList());
        log.debug(log_Prefix + "去重后集合:" + list.toString());
        return list;
    }

    // 判断字符串是否含中文
    public static boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    // 判断字符串是否含中文-自定义正则
    public static boolean isContainChinese(String str, String rgex) {

        Pattern p = Pattern.compile(rgex);
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    // 获取中文String[]
    public static List<String> getzh_CNKeys(String strInfo, String rgex) {

        List<String> zh_CNLists = new ArrayList<>();

        String patternString1 = KHUtil.isNull(rgex) ? "([\\u4e00-\\u9fa5|0-9]+)" : rgex;
        Pattern pattern = Pattern.compile(patternString1);
        Matcher matcher = pattern.matcher(strInfo);

        while (matcher.find()) {
            String cn_Key = matcher.group();
            zh_CNLists.add(cn_Key);
        }

        Iterator<String> iterator = zh_CNLists.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();

            if (!isContainChinese(next)) {
                iterator.remove();
            }
        }

        // 返回
        return zh_CNLists;
    }

    // 将字符串中中文片段替换为{{中文}}
    public static String doReplCNData2(String strInfo) {

        String cn_Info = strInfo;
        String patternString1 = "([\\u4e00-\\u9fa5|0-9]+)";
        Pattern pattern = Pattern.compile(patternString1);
        Matcher matcher = pattern.matcher(strInfo);

        int i = -1;
        int addNum = 0;
        while (matcher.find()) {
            i++;
            String cn_Key = matcher.group();

            if (KHDYNUtil.isContainChinese(cn_Key, "([\\u4e00-\\u9fa5]+)")) {

                int start = matcher.start() + addNum;
                int end = matcher.end() + addNum;

                log.debug(log_Prefix + "[" + i + "]1-中文片段:" + cn_Key + " 开始位置：" + start + " 结束位置：" + end);

                int beginNum = 0;
                int endNum = 0;
                if (start >= 2) {
                    beginNum = start - 2;
                } else if (start >= 1) {
                    beginNum = start - 1;
                } else {
                    beginNum = start;
                }
                if (cn_Info.length() - end >= 2) {
                    endNum = end + 2;
                } else if (cn_Info.length() - end >= 1) {
                    endNum = end + 1;
                } else {
                    endNum = end;
                }
                log.debug(log_Prefix + "[" + i + "]2-当前字符片段截取beginNum:" + beginNum + " endNum:" + endNum);

                String cn_Temp = cn_Info.substring(beginNum, endNum);
                log.debug(log_Prefix + "[" + i + "]3-现有字符片段前后增加2位截取的片段:" + cn_Temp);
                cn_Temp = cn_Temp.replaceAll("\\)", "-#").replaceAll("\\(", "#~").replaceAll("}", "-!").replaceAll("\\{", "!~").replaceAll("\\*", "~@");
                log.debug(log_Prefix + "[" + i + "]4-现有字符片段拓展2位-正则特殊字符替换:" + cn_Temp);

                String cn_After = "";

                if (cn_Temp.startsWith("!~!~") || cn_Temp.endsWith("-!-!")) {
                    log.debug(log_Prefix + "[" + i + "]5-检测到片段中已经出现语言转义符:" + cn_Temp + " 忽略处理");

                } else {
                    cn_After = cn_Temp.replaceAll(cn_Key, "{{" + cn_Key + "}}");
                    log.debug(log_Prefix + "[" + i + "]6-添加{{}}符之后的字符片段:" + cn_After);

                    cn_Info = cn_Info.replaceAll("\\)", "-#").replaceAll("\\(", "#~").replaceAll("}", "-!").replaceAll("\\{", "!~").replaceAll("\\*", "~@");
                    log.debug(log_Prefix + "[" + i + "]7-原始行数据将正则特殊字符替换:" + cn_Info);

                    cn_Info = cn_Info.replaceAll(cn_Temp, cn_After);
                    log.debug(log_Prefix + "[" + i + "]8-原始行数据将添加{{}}符后部分替换回:" + cn_Info);

                    cn_Info = cn_Info.replaceAll("-#", ")").replaceAll("#~", "(").replaceAll("-!", "}").replaceAll("!~", "{").replaceAll("~@", "*");
                    log.debug(log_Prefix + "[" + i + "]9-原始行数据将正则特殊字符替换内容换回:" + cn_Info);

                    addNum = addNum + 4;
                }

            } else {
                log.warn(log_Prefix + "[" + i + "]10-当前字符片段:" + cn_Key + " 未含中文");
            }
        }

        return cn_Info;
    }

}
